#include <cstdlib>
#include <string>
#include <iostream>
#include <vector>
#include <climits>

using namespace std;


struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

class Solution {
public:
	bool isBalanced(TreeNode* root, int& depth){
		if (root == NULL){
			depth = 0;
			return true;
		}
		TreeNode* leftChild = root->left;
		TreeNode* rightChild = root->right;
		int leftDepth = 0;
		int rightDepth = 0;
		bool isLeft = isBalanced(leftChild, leftDepth);
		bool isRight = isBalanced(rightChild, rightDepth);
		depth = (rightDepth > leftDepth ? rightDepth : leftDepth) + 1;
		return isLeft && isRight && abs(leftDepth - rightDepth) <= 1;
	}
	bool isBalanced(TreeNode *root) {
		/*
		if (root == NULL) return true;
		TreeNode* leftChild = root->left;
		TreeNode* rightChild = root->right;
		int leftDepth = depth(leftChild);
		int rightDepth = depth(rightChild);
		return isBalanced(leftChild) && isBalanced(rightChild) && abs(leftDepth - rightDepth) <= 1;
		*/
		int depth = 0;
		return isBalanced(root, depth);
	}
};
int main(int argc, char argv){

	return 0;
}